﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.Security;

namespace Coslen.TahorCRM.Security
{
    public class HttpCacheHelper
    {
        public static string GetUserPrincipalKey(string userName)
        {
            string result = string.Empty;
            result = "TahorSecurity_" + userName.ToLower().Replace(" ", "");
            return result;
        }

        public static void SetCachePrincipal(IPrincipal principal, bool createPersistentCookie, HttpContextBase context)
        {
            string cacheKey = HttpCacheHelper.GetUserPrincipalKey(principal.Identity.Name);
            context.Cache.Insert(cacheKey, principal);
            FormsAuthentication.SetAuthCookie(principal.Identity.Name, createPersistentCookie);
        }

        public static void SetCachePrincipal(IPrincipal principal, bool createPersistentCookie, HttpContext context)
        {
            string cacheKey = HttpCacheHelper.GetUserPrincipalKey(principal.Identity.Name);
            context.Cache.Insert(cacheKey, principal);
            FormsAuthentication.SetAuthCookie(principal.Identity.Name, createPersistentCookie);
        }
    }
}
